package system;

import com.xuly.ActivitiSpringBootApplication;
import com.xuly.common.ActivitiCommonService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Scanner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ActivitiSpringBootApplication.class)
@Slf4j
public class Main {
    @Autowired
    private ActivitiCommonService activitiCommonService;

    private Scanner scanner = new Scanner(System.in);

    @Test
    public void start() {
        while (true) {
            System.out.println("1.部署流程，2.发起流程，3.查找任务，4.完成任务，5.签收任务，0.退出");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    deploymentProcess();
                    break;
                case 2:
                    System.out.println("请输入流程实例key（或用默认leave_service）：");
                    String processInstanceKey = scanner.next();
                    String businessKey = null;
                    boolean flag = processInstanceKey != null && !"".equals(processInstanceKey.trim());
                    if (flag) {
                        System.out.println("请输入流程业务key（或用默认leave_business_key）：");
                        businessKey = scanner.next();
                    }
                    if (!flag) {
                        activitiCommonService.startProcessInstance("leave_service", "leave_business_key");
                    } else {
                        if (businessKey != null) {
                            activitiCommonService.startProcessInstance(processInstanceKey, businessKey);
                        } else {
                            activitiCommonService.startProcessInstance(processInstanceKey);
                        }
                    }
                    break;
                case 3:
                    findTask();
                    break;
                case 4:
                    System.out.println("请输入任务ID");
                    String taskId = scanner.next();
                    activitiCommonService.completeTask(taskId);
                    break;
                case 5:
                    break;
                case 0:
                    return;
                default:
                    System.out.println("请输入正确的编号！");
                    break;
            }
        }
    }

    private void deploymentProcess() {
        System.out.println("请输入要部署的流程(或者回车回车默认用leave)：");
        String model = scanner.next();
        String name = null;
        String resourceBpmn = null;
        switch (model) {
            case "leave":
                name = model;
                resourceBpmn = "processes/leave.bpmn";
                break;
            default:
                name = model;
                resourceBpmn = "processes/leave.bpmn";
                break;
        }
        if (name != null) {
            activitiCommonService.deploymentProcess(name, resourceBpmn);
        }
    }

    private void findTask() {
        while (true) {
            System.out.println("查找任务：1.根据代办人或候选人查找，2.根据流程实例ID查找，0.返回上一级");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    System.out.println("输入代办人或候选人姓名:");
                    String name = scanner.next();
                    activitiCommonService.findTaskByAssigneeOrCandidate(name);
                    return;
                case 2:
                    System.out.println("输入流程ID：");
                    String processInsId = scanner.next();
                    activitiCommonService.findTaskByProcInsId(processInsId);
                    return;
                case 0:
                    return;
                default:
                    break;
            }
        }
    }
}
